package org.core.tliaswebmanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.core.tliaswebmanagement.mapper.StudentMapper;
import org.core.tliaswebmanagement.pojo.PageResult;
import org.core.tliaswebmanagement.pojo.Student;
import org.core.tliaswebmanagement.pojo.StudentQueryParam;
import org.core.tliaswebmanagement.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> page(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Student> studentList = studentMapper.list(param);
        Page<Student> pageInfo = (Page<Student>) studentList;
        return new PageResult<Student>(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getById(id);}

    @Override
    public void update(Student student) {
        studentMapper.update(student);
    }

    @Override
    public void updateViolation(Integer id, Integer score) {
        studentMapper.updateViolationById(id,score);
    }
}
